package com.xyzwps.paimon.furina.infra

import org.springframework.boot.context.properties.ConfigurationProperties
import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.toDuration

@ConfigurationProperties(prefix = "app")
data class AppProperties(
    val session: SessionProperties
)

data class SessionProperties(val jwt: JwtProperties)

data class JwtProperties(val hs512key: String, val issuer: String, val expireHours: String) {
    fun expire(): Duration = run {
        try {
            val hours = expireHours.toLong()
            if (hours < 1) {
                throw RuntimeException("expire-hours cannot be less than 1")
            }
            hours.toDuration(DurationUnit.HOURS)
        } catch (e: NumberFormatException) {
            throw RuntimeException("expire-hours is invalid")
        }
    }
}